<!-- File: /app/views/users/show_users.ctp -->


<div class="users form">

	<h2>Show Users</h2>

	<!-- loop through $users array, displaying out user info -->
	<?php
	foreach ($users as $user) {
		$image = '';
		if (file_exists('img/profile/user/' . $user['User']['id'] . '.jpg'))
			$image = 'profile/user/' . $user['User']['id'] . '.jpg';
		else
			$image = '/img/default_user.jpg';?>

		<div class='user_displaying'><a href="profile/<?php echo $user['User']['id'];?>">
		<?php 
		echo $this->Html->image($image, array('width'=>100));
		echo "<p>".$user['User']['first_name'] . ' ' . $user['User']['last_name'] . "<p>";
		?></a><?php
		if ($curr_user['User']['id'] == $user['User']['id'])
			echo 'me';
		else {
			/* if the user is a friend of the curr session user ==> 'friends' */
			$already_friends = false;
			foreach ($friends as $friend) {
				if ($friend['Friend']['user1_id'] == $user['User']['id'] || $friend['Friend']['user2_id'] == $user['User']['id']) {
					$already_friends = true;
					echo 'friends';
					break;
				}
			}
			
			if (!$already_friends) {
				/* if the curr session user already requested the user to add him....
				 * $request_friends refers to a list of friend requests requested by the curr session user. 
				 */
				$requested = false;
				foreach ($request_friends as $request_friend) {   
					if ($request_friend['RequestFriend']['user_id'] == $user['User']['id']) {
							$requested = true;
							echo 'add friends';
							break;
					}
				}
				if (!$requested) 
					echo $this->Html->link('add friends', array('controller'=>'friends', 'action'=>'request_friends', $user['User']['id']));
			} 
		}
		?></div>
	
	<?php 	
	}
	?>



</div>
